package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.domain.PageResult;
import com.leyou.common.exception.domain.ExceptionEnum;
import com.leyou.common.exception.domain.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.domain.Brand;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> brandPageQuery(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //封装分页条件
        PageHelper.startPage(page, rows);
        //创建一个封装条件的对象
        Example example = new Example(Brand.class);
        //判断查询条件是否有值
        if(!StringUtils.isEmpty(key)){
            Example.Criteria criteria = example.createCriteria();
            //先把搜索的关键字都转成大写去精确匹配letter字段
            criteria.orEqualTo("letter", key.toUpperCase());
            //或者拿搜索关键字去模糊查询name名称
            criteria.orLike("name", "%"+key+"%");
        }
        if(!StringUtils.isEmpty(sortBy)){
            //setOrderByClause中可以写原始sql语句，比如： select * from xx order by a desc
            example.setOrderByClause(sortBy+" "+(desc ? "DESC" : "ASC"));
        }
        //分页查询
        List<Brand> list = brandMapper.selectByExample(example);
        //判断是否有数据
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //封装成PageHelper的分页对象
        PageInfo<Brand> pageInfo = new PageInfo<>(list);
        //获取PageHelper的分页对象中的list
        List<Brand> brands = pageInfo.getList();
        //转成dto的集合
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);
        //封装自定义的分页对象
        PageResult<BrandDTO> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getPages(), brandDTOS);
        return pageResult;
    }

    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        //把dto对象转成domain
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        //保存品牌数据
        try {
            brandMapper.insertSelective(brand);
        }catch (Exception e){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //获取新增品牌对象id,此刻Brand对象中除了image，其余属性都有值
        Long brandId = brand.getId();
        //保存中间表
        brandMapper.insertBrandAndCategory(cids, brandId);
    }

    public BrandDTO findBrandById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    public List<BrandDTO> findBrandsByCid(Long id) {
        //根据分类id查询出品牌的集合
        List<Brand> list =  brandMapper.findBrandsByCid(id);
        //判空
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
